#!/usr/bin/env python
import subprocess
import re
import time
import random
VERSION = "2023-10-10"

device_name="localhost:5555"
# 启动ADB连接
adb_devices = subprocess.run(["adb", "devices"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if  ":5555" not in adb_devices.stdout.decode() :
    adb_tcpip = subprocess.run(["adb", "forward", "tcp:5555","tcp:5555"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if "error" in adb_tcpip.stderr.decode() or "refused" in adb_tcpip.stderr.decode():
        print("未连接设备")
        exit(0)
    adb_connect = subprocess.run(["adb", "connect", device_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if "error" in adb_connect.stdout.decode() or "cannot" in adb_connect.stdout.decode():
        print("adb", "connect", device_name ,"出错")
        exit(0)

# 启动快手界面
runresult=subprocess.run(["adb", "-s", device_name, "shell", "am", "start", "com.kuaishou.nebula/com.yxcorp.gifshow.HomeActivity"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if "not found" in runresult.stdout.decode() or "not found" in runresult.stderr.decode():
    print("启动应用不成功")
    exit(0)
# 检查当前界面是否是快手推荐朋友
def is_acitvity_kuaishou_friend():
    current_activity = subprocess.getoutput("adb -s localhost:5555 shell \"dumpsys activity | grep ActivityRecord | head -n 1\"")
    if "explore.activity.ExploreFriendActivity" in current_activity:
        return True
    return False

# 从UI XML中提取并点击坐标
def read_xml_and_click(ui_name,keyword):
    with open(ui_name, 'r', encoding='utf-8') as file:
        text = file.read()

    array_x = []
    array_y = []

    # 使用正则表达式来匹配 "更多" 后面的 "bounds" 值
    #pattern = r'.*?bounds="(\[\d+,\d+\]\[\d+,\d+\])"'
    pattern = keyword + r'.*?bounds="(\[\d+,\d+\]\[\d+,\d+\])"'
    boundsMatches = re.findall(pattern, text)

    # 提取匹配到的 "bounds" 值
    for bounds in  boundsMatches:
        # 使用正则表达式来匹配坐标值
        pattern = r'\[(\d+),(\d+)\]\[(\d+),(\d+)\]'
        matchesPos = re.match(pattern, bounds)

        if matchesPos:
            x1, y1, x2, y2 = map(int, matchesPos.groups())

            x_point = (x1 + x2) / 2 +random.randint(0, 4)
            y_point = (y1 + y2) / 2 +random.randint(0, 4)
            if(x_point>400):
                array_x.append(x_point)
                array_y.append(y_point)
                print(f'x_point: {x_point}, y_point: {y_point}')
        else:
            print("未找到匹配的 bounds 值")



    for i in range(0, len(array_x) , 1):
        x = array_x[i]
        y = array_y[i]
        print(f'开始点击关注按钮: {x},  {y}')
        subprocess.run(["adb", "-s", device_name, "shell", "input", "tap", str(x), str(y)])
        time.sleep(1)


    if is_acitvity_kuaishou_friend()==0:
            subprocess.run(["adb", "-s", device_name, "shell", "input", "keyevent", "BACK"])
            time.sleep(1)
            if is_acitvity_kuaishou_friend() == 0:
                subprocess.run(["adb", "-s", device_name, "shell", "input", "keyevent", "BACK"])
                time.sleep(1)
            dis = 120 * i + 360
            subprocess.run(["adb", "-s", device_name, "shell", "input", "swipe", "300", str(dis), "310", "200"])
            time.sleep(2)

    #滑动一下
    subprocess.run(["adb", "-s", device_name, "shell", "input", "swipe", "300", "1300", "310", "200"])
    time.sleep(2)




def pull_screen_ui(ui_path):

    if is_acitvity_kuaishou_friend() == 1:
        # 获取当前界面的ui.xml
        subprocess.run(["adb", "-s", device_name, "shell", "uiautomator", "dump", ui_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        subprocess.run(["adb", "-s", device_name, "pull", ui_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)

while is_acitvity_kuaishou_friend()==1:
    for m in range(1001):
        pull_screen_ui("/sdcard/ui.xml")
        print("滑屏幕", m)
        read_xml_and_click('ui.xml','text="关注')
    print("退出 for")
else:
    print("请先进入\"快手\"再点击\"快手推荐朋友\"再运行该程序")
